''' <summary>
''' Class to read Paradox DB files without needing BDE to be installed
''' It does this by simply exporting the table to a text file and then using the text file reader on that file
''' </summary>
Public Class clsDataParadox
    Inherits clsDataText
    Implements IDisposable

    Dim txtFilename As String = ""

    ''' <summary>
    ''' Instantiate clsData, assign filename, open file, set filelength and fieldnames
    ''' </summary>
    ''' <param name="_Filename">Name of text file to read</param>
    Public Overrides Sub Initialize(ByVal _Filename As String)
        If Not ExportTable(_Filename, txtFilename) Then Exit Sub
        MyBase.Initialize(txtFilename)
    End Sub

    'following was copied from DBParadox to avoid having to do a reference to those libraries for a couple of routines
    ''' <summary>
    ''' Create tab-delimited text file and export table to it
    ''' </summary>
    ''' <param name="PDoxFileName">Name of file to export</param>
    ''' <param name="ExportFileName">Name of file to export to (if blank, will export to temp file and return name here; caller must place in clipboard and delete tempfile)</param>
    ''' <returns>True if export successful</returns>
    ''' <remarks>Override for Paradox</remarks>
    Private Function ExportTable(ByVal PDoxFileName As String, ByRef ExportFileName As String) As Boolean
        Dim fs As IO.FileStream = Nothing
        Dim br As IO.BinaryReader = Nothing
        Dim sw As IO.StreamWriter = Nothing
        Dim Hdr As PDoxHeader = Nothing
        Dim Hdr2 As PDoxHeader2 = Nothing

        Dim NumRecords As Long = modParadox.NumRecords(PDoxFileName)

        Try
            If Not My.Computer.FileSystem.FileExists(PDoxFileName) Then
                Throw New IO.FileNotFoundException("File not found", PDoxFileName)
                Return False
            End If

            fs = New IO.FileStream(PDoxFileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read, 1024)
            br = New IO.BinaryReader(fs)

            Dim TempDir As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\WRDB\"
            If Not My.Computer.FileSystem.DirectoryExists(TempDir) Then My.Computer.FileSystem.CreateDirectory(TempDir)

            If ExportFileName = "" Then ExportFileName = TempDir & IO.Path.ChangeExtension(IO.Path.GetFileName(PDoxFileName), "txt")

            sw = New IO.StreamWriter(ExportFileName, False)

            'read header

            If Not GetHeader(br, Hdr, Hdr2) Then Return False

            With Hdr2

                'write column headings and read each data block

                For f As Integer = 0 To Hdr.numFields - 1
                    If f > 0 Then sw.Write(vbTab)
                    sw.Write(.fieldName(f))
                Next
                sw.WriteLine()

                Dim Block As PDoxDataBlock = Nothing
                Dim BlockNum As Integer = 0
                Dim RecNum As Integer = 0

                While Hdr.fileBlocks > 0 AndAlso GetNextRecord(br, Hdr, Hdr2, Block, BlockNum)
                    For f As Integer = 0 To modParadox.mItems.Count - 1
                        If f > 0 Then sw.Write(vbTab)
                        If TypeOf modParadox.mItems(f) Is Date Then
                            sw.Write(String.Format("{0:MM/dd/yyyy HH:mm}", modParadox.mItems(f)))
                        ElseIf TypeOf modParadox.mItems(f) Is Boolean Then
                            sw.Write(IIf(modParadox.mItems(f), "1", "0"))
                        Else
                            sw.Write(modParadox.mItems(f))
                        End If
                    Next
                    sw.WriteLine()
                    RecNum += 1
                End While
            End With
            Return True
        Catch ex As Exception
            ExportTable = False
            Throw New ApplicationException(String.Format("Unable to export from Paradox file ({0}); the file may be corrupted.", PDoxFileName), ex)
        Finally
            If sw IsNot Nothing Then
                sw.Close()
                sw.Dispose()
                br.Close()
                fs.Close()
                fs.Dispose()
            End If
            Hdr = Nothing
            Hdr2 = Nothing
        End Try
    End Function

End Class
